<template>
  <div id="totop" @click="toTop" v-show="isShow">去顶部</div>
</template>

<script>
  export default {
    data () {
      return {
        top: 0
      }
    },
    mounted () {
      window.addEventListener('scroll', () => {
        this.top = document.documentElement.scrollTop
      })
    },
    computed: {
      isShow () {
        return this.top === 0 ? false : true
      }
    },
    methods: {
      toTop () {
        // 此处用document.body不行，http://www.jb51.net/article/21168.htm
        document.documentElement.scrollTop = 0
      }
    }
  }
</script>

<style>
  #totop{
    position: fixed;
    right:100px;
    bottom:100px;
    cursor: pointer;
  }
</style>
